#!/bin/bash
if ! which make &> /dev/null; then
    echo "[ERROR] Invalid command: make\n"
    exit 1
fi

app="kirin"
suffix=".exe"

input="$app$suffix"
goal_dir="build"

if [ ! -d $goal_dir ]; then
    if ! mkdir $goal_dir; then
        exit 2
    fi
fi

archs="x86-64-vnni512 x86-64-vnni256 x86-64-avx512 x86-64-avx512f x86-64-avxvnni x86-64-bmi2 x86-64-avx2 x86-64-sse41-popcnt x86-64-modern x86-64-ssse3 x86-64-sse3-popcnt x86-64"
for arch in $archs; do
    output="$goal_dir/$app-$arch$suffix"
    if [ -f $output ]; then
        continue
    fi
    make clean
    if make -j build ARCH=$arch > /dev/null; then
        if which upx &> /dev/null; then
            upx -9 $input &> /dev/null
        fi
        mv $input $output
    else
        echo "Stopped!"
        exit 3
    fi
done
echo "Over!"
